/**
 * Copyright (c) 2011-2013, kidzhou 周磊 (zhouleib1412@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hpclub.server.ext.fastexcel;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.ext.fastexcel.FastExcelRender.AppendMoreData;
import cn.hpclub.server.util.fastexcel.Workbook;
import cn.hpclub.server.util.fastexcel.Worksheet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class FastExcelExporter {

    public static final String VERSION_2003 = "2003";
    private static final int HEADER_ROW = 1;
    private static final int MAX_ROWS = 65535;
    private String version;
    private String[] sheetNames = new String[]{"sheet"};
    private int cellWidth = 8000;
    private int headerRow;
    private String[][] headers;
    private String[][] columns;
    private List<?>[] data;
    private String application;
    private OutputStream os;
    private AppendMoreData append;
    public FastExcelExporter(List<?>... data) {
        this.data = data;
    }

    public static FastExcelExporter data(List<?>... data) {
        return new FastExcelExporter(data);
    }

    public static List<List<?>> dice(List<?> num, int chunkSize) {
        int size = num.size();
        int chunk_num = size / chunkSize + (size % chunkSize == 0 ? 0 : 1);
        List<List<?>> result = Lists.newArrayList();
        for (int i = 0; i < chunk_num; i++) {
            result.add(Lists.newArrayList(num.subList(i * chunkSize, i == chunk_num - 1 ? size : (i + 1) * chunkSize)));
        }
        return result;
    }

    public void export() throws IOException {
        if(data == null && append != null) {
            set(this.append.append());
        }
        Preconditions.checkNotNull(data, "data can not be null");
        Preconditions.checkNotNull(headers, "headers can not be null");
        Preconditions.checkNotNull(columns, "columns can not be null");
        Preconditions.checkArgument(data.length == sheetNames.length && sheetNames.length == headers.length
                && headers.length == columns.length, "data,sheetNames,headers and columns'length should be the same." +
                "(data:" + data.length + ",sheetNames:" + sheetNames.length + ",headers:" + headers.length + ",columns:" + columns.length + ")");
        Preconditions.checkArgument(cellWidth >= 0, "cellWidth can not be less than 0");
        Workbook wb = new Workbook(os, application, version);
        
        if (data.length == 0) {
            return;
        }
        for (int i = 0; i < data.length; i++) {
            Worksheet ws = wb.newWorksheet(sheetNames[i] == null ? "Sheet " + i : sheetNames[i]);
            if (headers[i].length > 0) {
                if (headerRow <= 0) {
                    headerRow = HEADER_ROW;
                }
                headerRow = Math.min(headerRow, MAX_ROWS);
                for (int h = 0, lenH = headers[i].length; h < lenH; h++) {
                    ws.value(0, h,headers[i][h]);
                }
            }
            List<?> temp = data[i];
            int current = 0;
            do {
                for (int j = 0, len = temp.size(); j < len; j++) {               
                    Object obj = temp.get(j);
                    if (obj == null) {
                        continue;
                    }
                    if (obj instanceof Map) {
                        processAsMap(columns[i], ws,j + headerRow + current, obj);
                    } else if (obj instanceof Model) {
                        processAsModel(columns[i], ws,j + headerRow + current, obj);
                    } else if (obj instanceof Record) {
                        processAsRecord(columns[i], ws,j + headerRow + current, obj);
                    } else {
                        throw new RuntimeException("Not support type[" + obj.getClass() + "]");
                    }
                }
                current += temp.size();
            }while(append != null && (temp = append.append()) != null);
        }
        wb.finish();
    }

    @SuppressWarnings("unchecked")
    private static void processAsMap(String[] columns, Worksheet ws, int index,Object obj) {
        Map<String, Object> map = (Map<String, Object>) obj;
        if (columns.length == 0) { // show all if column not specified
            Set<String> keys = map.keySet();
            int columnIndex = 0;
            for (String key : keys) {
                ws.value(index,columnIndex, map.get(key));
                columnIndex++;
            }
        } else {
            for (int j = 0, len = columns.length; j < len; j++) {
                ws.value(index,j,map.get(columns[j]) == null ? "" : map.get(columns[j]));
            }
        }
    }

    private static void processAsModel(String[] columns, Worksheet ws, int index, Object obj) {
        Model<?> model = (Model<?>) obj;
        Set<Entry<String, Object>> entries = model.getAttrsEntrySet();
        if (columns.length == 0) { // show all if column not specified
            int columnIndex = 0;
            for (Entry<String, Object> entry : entries) {
                ws.value(index,columnIndex, entry.getValue());
                columnIndex++;
            }
        } else {
            for (int j = 0, len = columns.length; j < len; j++) {
                ws.value(index,j,model.get(columns[j]) == null ? "" : model.get(columns[j]));
            }
        }
    }

    private static void processAsRecord(String[] columns, Worksheet ws, int index, Object obj) {
        Record record = (Record) obj;
        Map<String, Object> map = record.getColumns();
        if (columns.length == 0) { // show all if column not specified
            record.getColumns();
            Set<String> keys = map.keySet();
            int columnIndex = 0;
            for (String key : keys) {
                ws.value(index,columnIndex, record.get(key));
                columnIndex++;
            }
        } else {
            for (int j = 0, len = columns.length; j < len; j++) {
                ws.value(index,j,map.get(columns[j]) == null ? "" : map.get(columns[j]));
            }
        }
    }
    public FastExcelExporter application(String name) {
        this.application = name;
        return this;
    }
    public FastExcelExporter os(OutputStream os) {
        this.os = os;
        return this;
    }
    
    public FastExcelExporter version(String version) {
        this.version = version;
        return this;
    }

    public FastExcelExporter sheetName(String sheetName) {
        this.sheetNames = new String[]{sheetName};
        return this;
    }

    public FastExcelExporter sheetNames(String... sheetName) {
        this.sheetNames = sheetName;
        return this;
    }

    public FastExcelExporter cellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
        return this;
    }

    public FastExcelExporter headerRow(int headerRow) {
        this.headerRow = headerRow;
        return this;
    }

    public FastExcelExporter header(String... header) {
        this.headers = new String[][]{header};
        return this;
    }

    public FastExcelExporter headers(String[]... headers) {
        this.headers = headers;
        return this;
    }

    public FastExcelExporter column(String... column) {
        this.columns = new String[][]{column};
        return this;
    }

    public FastExcelExporter columns(String[]... columns) {
        this.columns = columns;
        return this;
    }

    public FastExcelExporter set(List<?>... data) {
        this.data = data;
        return this;
    }
    public FastExcelExporter append(AppendMoreData append){
        this.append = append;
        return this;
    }

}
